﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// object that holds data from folder sync task
    /// </summary>
    [Serializable]
    public class FolderSyncOutput:ResultBase
    {
        private int _FilesChanged;
        /// <summary></summary>
        public int FilesChanged
        {
            get
            {
                return this._FilesChanged;
            }
            set
            {
                this._FilesChanged = value;
            }
        }

        private int _FilesCreated;
        /// <summary></summary>
        public int FilesCreated
        {
            get
            {
                return this._FilesCreated;
            }
            set
            {
                this._FilesCreated = value;
            }
        }

        private int _FilesDeleted;
        /// <summary></summary>
        public int FilesDeleted
        {
            get
            {
                return this._FilesDeleted;
            }
            set
            {
                this._FilesDeleted = value;
            }
        }

        /// <summary>
        /// ctor
        /// </summary>
        public FolderSyncOutput():base(Guid.Empty,Guid.Empty)
        {
            this._FilesDeleted = 0;
            this._FilesCreated = 0;
            this._FilesChanged = 0;
        }

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="newFiles"></param>
        /// <param name="modifiedFiles"></param>
        /// <param name="deletedFiles"></param>
        public FolderSyncOutput(Guid componentID, Guid jobTicketID,
            int newFiles,int modifiedFiles,int deletedFiles) 
            : base(componentID, jobTicketID)
        {
            this._FilesDeleted = deletedFiles;
            this._FilesCreated = newFiles;
            this._FilesChanged = modifiedFiles;
        }

        /// <summary>
        /// serialization
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"FilesCreated",this.FilesCreated.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilesChanged", this.FilesChanged.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"FilesDeleted",this.FilesDeleted.ToString());

            return xNode;
        }

        /// <summary>
        /// deserialize
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FolderSyncOutput output = new FolderSyncOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.FilesCreated = int.Parse(xNode.Attributes.GetNamedItem("FilesCreated").Value);
                output.FilesChanged = int.Parse(xNode.Attributes.GetNamedItem("FilesChanged").Value);
                output.FilesDeleted = int.Parse(xNode.Attributes.GetNamedItem("FilesDeleted").Value);

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }
    }
}
